package psnl.bingo.mr.demo2;

import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
import psnl.bingo.mr.demo1.D1Line;

import java.io.IOException;

public class D2Reducer extends Reducer<D2Line, NullWritable, Text, NullWritable> {
    @Override
    protected void reduce(D2Line key, Iterable<NullWritable> values, Context context) throws IOException, InterruptedException {
        // values对应Mapper输出的k-v键值对儿中，某一个分组的v的数据（values对应的key是相同的）

        String city=null, obj=null, year=null;
        int dataSum = 0;

        for (NullWritable v : values) {
            if(city == null) { // 得到key对象中，相同属性的信息
                city = key.getCity();
                obj = key.getObj();
                year = key.getYear();
            }

            dataSum += key.getData();;
        }

        String outStr = String.join(";", city, obj, year, dataSum+"");

        context.write(new Text(outStr), NullWritable.get());
    }
}
